
package com.grt192.demos;

import com.grt192.actuator.exception.ActuatorException;
import com.grt192.core.GRTObject;
import com.grt192.event.SensorChangeListener;
import com.grt192.event.SensorEvent;
import com.grt192.event.component.DemoSwitchListener;
import com.grt192.event.component.SwitchListener;
import com.grt192.spot.actuator.GRTDemoLED;
import com.grt192.spot.actuator.GRTLogicRelay;
import com.grt192.spot.mechanism.MaxbotixSoftConstantLoop;
import com.grt192.spot.mechanism.hauntedHouse.StatusLight;
import com.grt192.spot.sensor.GRTDemoSwitch;
import com.grt192.spot.sensor.GRTPulseSwitch;
import com.grt192.spot.sensor.GRTSonar;
import com.grt192.spot.sensor.GRTSwitch;
import com.sun.spot.resources.transducers.IIOPin;
import com.sun.spot.resources.transducers.IInputPin;
import com.sun.spot.sensorboard.EDemoBoard;

/**
 * This demo continuously polls sonars. This is fine because their threads hang
 * until they receive a pulse.
 *
 * Rx will pulse on sw1 switch release.
 * We will read the output from the last sonar as a switch.
 * @author ajc
 */
public class MaxbotixCommandLoopDemo extends GRTObject implements SwitchListener, SensorChangeListener, DemoSwitchListener {

    GRTSonar[] sonars;
//    GRTLogicRelay rx;
    IIOPin rx;
    GRTSwitch s;
    GRTDemoSwitch p1;
    StatusLight status;

    MaxbotixSoftConstantLoop c;

    public MaxbotixCommandLoopDemo() {
        status = new StatusLight(new GRTDemoLED(0));
        status.rawColor(GRTDemoLED.Color.ORANGE);
        setPrinting(true);

        log("building rx pin on d4...");
        rx = EDemoBoard.getInstance().getIOPins()[4];
//        rx = new GRTLogicRelay(4);
//        try {
//            rx.doCommand(GRTLogicRelay.LOW);
//        } catch (ActuatorException ex) {
//            ex.printStackTrace();
//        }

        log("building sonars...");
        sonars = new GRTSonar[1];
        for (int i = 0; i < sonars.length; i++) {
            sonars[i] = GRTSonar.fromAnalog(i , 50, "a" + i );
            sonars[i].addSensorChangeListener(this);
            sonars[i].start();//do we want to start?
        }

        log("building pulseSwitch on d0...");
        GRTPulseSwitch p = new GRTPulseSwitch(0, 5, "pswitch");

        c = new MaxbotixSoftConstantLoop(sonars, rx, p);
        c.startRanging();
//        c.start();

//        log("building switch on d4...");
//        s = new GRTSwitch(4, 50, "d4");
//        s.addSwitchListener(this);
//        s.start();

        log("building demoswitch on sw1...");
        p1 = new GRTDemoSwitch(0, 50, "sw1");
        p1.addSwitchListener(this);
        p1.start();

        log("All systems go!");
        status.rawColor(GRTDemoLED.Color.GREEN);

        //replaced by GRTPulseSwitch
//        EDemoBoard spotBoard = EDemoBoard.getInstance();
//        IInputPin d3 = spotBoard.getIOPins()[3];
//        while(true){
//            log("pulse..." + spotBoard.getPulse(d3, true, 0));
//
//        }
    }

//    protected void pulseRx() {
//        try {
//            rx.doCommand(GRTLogicRelay.HIGH);//300uS
//            rx.doCommand(GRTLogicRelay.LOW);
//        } catch (ActuatorException ex) {
//            ex.printStackTrace();
//        }
//    }

    public void switchPressed(GRTSwitch source) {
        log("Received pulse from sonar TX... stand by for end of pulse");
    }

    public void switchReleased(GRTSwitch source) {
        log("Received end of pulse from sonar TX");
    }

    public void switchPressed(GRTDemoSwitch source) {
        log(source.getId() + ": \t PRESSED");
    }

    public void switchReleased(GRTDemoSwitch source) {
        log(source.getId() + ": \t RELEASED");
        log("sending pulse...");
        status.blinkonBlack();
//        pulseRx();
//        c.pulseRx();
    }

    public void sensorStateChanged(SensorEvent e, String key) {
        log(e.getSource().getId() + ":\t" + key + ":\t" + e.getData());
    }
}
